package com.tpshion.chat.exception;

import com.tpshion.chat.utils.JsonUtil;
import java.io.Serializable;

public class ResponseVo<T> implements Serializable {

    /**
     * 业务状态
     */
    private Boolean status;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 描述信息
     */
    private String message;

    /**
     * 数据体
     */
    private T data;

    /**
     * 时间戳
     */
    private Long timestamp = System.currentTimeMillis();

    /**
     * 无参构造私有化
     */
    private ResponseVo(){

    }

    /**
     * 全参构造
     * @param status
     * @param code
     * @param message
     * @param data
     */
    public ResponseVo(Boolean status, Integer code, String message, T data){
        this.status = status;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 构建实体
     * @param status
     * @param respStatus
     * @param data
     * @param <T>
     * @return
     */
    private static <T> ResponseVo<T> simpleBuild(Boolean status, RespStatus respStatus, T data){
        return new ResponseVo<T>(status, respStatus.getCode(), respStatus.getMessge(), data);
    }

    /**
     * 封装成功返回
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResponseVo<T> ok(T data){
        return simpleBuild(true,RespStatus.OK,data);
    }

    /**
     * 封装错误返回
     * @param message
     * @return
     */
    public static ResponseVo error(String message){
        return simpleBuild(false, RespStatus.FAIL, null);
    }

    /**
     * 封装返回构建
     * @param respStatus
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ResponseVo<T> build(RespStatus respStatus, T data){
        return simpleBuild(true, respStatus, data);
    }

    /**
     * 业务异常返回封装
     * @param e
     * @param <T>
     * @return
     */
    public static <T> ResponseVo<T> buildException(MyException e){
        return new ResponseVo<T>(false,e.getCode(),e.getMessage(),null);
    }

    /**
     * 系统异常返回封装
     * @param respStatus
     * @param <T>
     * @return
     */
    public static <T> ResponseVo<T> buildSystemException(RespStatus respStatus){
        return simpleBuild(false, respStatus, null);
    }

    /**
     * 当前对象转为json字符串
     * @return
     */
    public String jsonStr(){
        return JsonUtil.obj2String(this);
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }
}
